import React, { PureComponent } from 'react'

export default class Optimize extends PureComponent {
  state = { carName: '奔驰C63' }

//   shouldComponentUpdate(nextProps, nextState) {
//     console.log(this.props, this.state)
//     console.log(nextProps, nextState)
//     if (this.state.carName === nextState.carName) {
//       return false
//     } else {
//       return true
//     }
//   }

  changeCar = () => {
    this.setState({ carName: '奔驰C6' })
  }

  render() {
    const { carName } = this.state
    return (
      <div>
        Father
        <span>车名：{carName}</span>
        <button onClick={this.changeCar}>换车</button>
        <Child carName="宝马" />
      </div>
    )
  }
}

class Child extends PureComponent {
//   shouldComponentUpdate(nextProps, nextState) {
//     if (this.props.carName === nextProps.carName) {
//       return false
//     } else {
//       return true
//     }
//   }

  render() {
    console.log('>是否调用')
    const { carName } = this.props
    return (
      <div>
        子组件
        <span>父组件的传值：{carName}</span>
      </div>
    )
  }
}
